package cn.group.hotel2.service.impl;

import cn.group.hotel2.ex.ServiceException;
import cn.group.hotel2.mapper.OrderMapper;
import cn.group.hotel2.mapper.RoomCheckInMapper;
import cn.group.hotel2.pojo.dto.RoomCheckInAddNewDTO;
import cn.group.hotel2.pojo.dto.RoomCheckInUpdateDTO;
import cn.group.hotel2.pojo.entity.RoomCheckIn;
import cn.group.hotel2.pojo.vo.*;
import cn.group.hotel2.service.IRoomCheckInService;
import cn.group.hotel2.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.List;

/**
 * 处理入住登记数据相关的业务实现类
 *
 * @author wcl
 * @version 0.0.1
 * @date 2022/11/21
 */
@Slf4j
@Service
public class RoomCheckInServiceImpl implements IRoomCheckInService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RoomCheckInMapper roomCheckInMapper;

    public RoomCheckInServiceImpl() {
        log.debug("创建业务类对象:RoomCheckInServiceImpl");
    }

    @Override
    public void addNew(RoomCheckInAddNewDTO roomCheckInAddNewDTO) {
        log.debug("开始处理【新增入住登记】的业务,参数:{}", roomCheckInAddNewDTO);

        String orderNum = roomCheckInAddNewDTO.getOrderNum();
        OrderStandardVO queryResult = orderMapper.getStandardByOrderNum(orderNum);
        log.debug("根据订单编号【{}】获取订单详情,结果为:{}", orderNum, queryResult);
        if(queryResult == null) {
            String message = "新增入住登记失败,该订单编号不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_ORDER_NOT_FOUND, message);
        }
        log.debug("订单编号存在!");

        String name = roomCheckInAddNewDTO.getName();
        int count = orderMapper.countByName(name);
        log.debug("根据订单的客户姓名【{}】查询订单表的数据是否存在,结果为:{}", name, count);
        if(count < 1) {
            String message = "新增入住登记失败,该订单的客户姓名错误";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NAME_NOT_FOUND, message);
        }
        log.debug("该客户的姓名存在!");

        String phone = roomCheckInAddNewDTO.getPhone();
        count = orderMapper.countByPhone(phone);
        log.debug("根据订单的客户手机号【{}】查询订单表的数据是否存在,结果为:{}", phone, count);
        if(count < 1) {
            String message = "新增入住登记失败,该订单的客户手机号错误";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_PHONE_NOT_FOUND, message);
        }
        log.debug("该客户的手机号存在");

        String ID = roomCheckInAddNewDTO.getIdentityCard();
        count = orderMapper.countByIdentityCard(ID);
        log.debug("根据订单的客户深得中【{}】查询订单表的数据是否存在,结果为:{}", ID, count);
        if(count < 1) {
            String message = "新增入住登记失败,该订单的客户身份证错误";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_PHONE_NOT_FOUND, message);
        }
        log.debug("该客户的身份证存在");

        Long orderId = queryResult.getId();
        LocalDateTime checkInTime = LocalDateTime.now();
        Integer isPay = roomCheckInAddNewDTO.getIsPay();
        RoomCheckIn roomCheckIn = new RoomCheckIn();
        roomCheckIn.setIsPay(isPay);
        roomCheckIn.setOrderId(orderId);
        roomCheckIn.setCheckInTime(checkInTime);
        log.debug("即将向入住登记表中插入数据,结果为:{}", roomCheckIn);
        int rows = roomCheckInMapper.insert(roomCheckIn);
        if(rows != 1) {
            String message = "新增入住登记失败,系统繁忙,请稍后再次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("新增完成!");

    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据记录id删除入住登记单条数据】的业务,参数:{}", id);

        RoomCheckInStandardVO queryResult = roomCheckInMapper.getStandardById(id);
        log.debug("根据id【{}】查询入住登记详情,结果为:{}", id, queryResult);
        if (queryResult == null) {
            String message = "删除入住登记失败,该条数据不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        log.debug("即将删除该条入住登记");
        int rows = roomCheckInMapper.deleteById(id);
        if(rows != 1) {
            String message = "删除入住登记失败,系统繁忙,请稍后再次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
        log.debug("删除完成!");
    }

    @Override
    public void updateById(Long id, RoomCheckInUpdateDTO roomCheckInUpdateDTO) {
        log.debug("开始处理【根据订单id修改入住登记信息】的业务,参数:{},{}", id, roomCheckInUpdateDTO);

        String updateOrderNum = roomCheckInUpdateDTO.getOrderNum();
        List<RoomCheckInListItemVO> list = roomCheckInMapper.list();
        Iterator<RoomCheckInListItemVO> iterator = list.iterator();
        while (iterator.hasNext()) {
            RoomCheckInListItemVO item = iterator.next();
            if(item.getOrderNum().equals(updateOrderNum)) {
                iterator.remove();
                break;
            }
        }
        log.debug("移除自身数据后的入住登记列表:{}", list);
        for (RoomCheckInListItemVO roomCheckInListItemVO : list) {
            String orderNumListItemVO = roomCheckInListItemVO.getOrderNum();
            if(orderNumListItemVO.equals(updateOrderNum)){
                String message = "修改入住登记详情失败,该订单号已存在！";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }

        OrderStandardVO queryResult = orderMapper.getStandardByOrderNum(updateOrderNum);
        if (queryResult == null) {
            String message = "修改入住登记失败,该条入住登记信息不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_ORDER_NOT_FOUND, message);
        }

        Long orderId = queryResult.getId();
        CostumerStandardVO costumerQueryResult = roomCheckInMapper.getStandardByOrderId(orderId);
        String updateName = roomCheckInUpdateDTO.getName();
        String name = costumerQueryResult.getName();
        log.debug("根据订单id查询客户信息,修改的姓名:{},订单表的客户姓名:{}", updateName, name);
        if(!updateName.equals(name)) {
            String message = "修改入住登记失败,修改的姓名与对应的订单表的姓名不一致";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NAME_NOT_FOUND, message);
        }

        String phone = costumerQueryResult.getPhone();
        String updatePhone = roomCheckInUpdateDTO.getPhone();
        log.debug("根据订单id查询客户信息,修改的手机号:{},订单表的客户手机号:{}", updatePhone, phone);
        if(!updatePhone.equals(phone)) {
            String message = "修改入住登记失败,修改的手机号与对应的订单表的手机号不一致";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_PHONE_NOT_FOUND, message);
        }

        String identityCard = costumerQueryResult.getIdentityCard();
        String updateIdentityCard = roomCheckInUpdateDTO.getIdentityCard();
        log.debug("根据订单id查询客户信息,修改的身份证:{},订单表的客户身份证:{}", updateIdentityCard, identityCard);
        if(!updateIdentityCard.equals(identityCard)) {
            String message = "修改入住登记失败,修改的身份证与对应的订单表的身份证不一致";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_ID_NOT_FOUND, message);
        }

        RoomCheckIn roomCheckIn = new RoomCheckIn();
        Integer isPay = roomCheckInUpdateDTO.getIsPay();
        roomCheckIn.setId(id);
        roomCheckIn.setIsPay(isPay);
        roomCheckIn.setOrderId(orderId);
        log.debug("即将修改入住登记信息,结果为:{}", roomCheckIn);
        int rows = roomCheckInMapper.update(roomCheckIn);
        if(rows != 1) {
            String message = "修改入住登记失败,系统繁忙,请稍后再次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
        log.debug("修改入住登记信息完成!");
    }

    @Override
    public RoomCheckInStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据入住登记id查询登记数据】的业务,参数:{}", id);

        RoomCheckInStandardVO queryResult = roomCheckInMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "查询登记数据失败,该条数据不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        log.debug("查询完成,结果为:{}",queryResult);
        return queryResult;
    }


    @Override
    public List<RoomCheckInListItemVO> list() {
        log.debug("开始处理【查询入住登记列表】的业务,无参数");
        return roomCheckInMapper.list();
    }

    @Override
    public List<RoomCheckInListItemVO> listByPageNum(Long pageNum) {
        log.debug("开始处理【根据所选的页数来分页查询】的业务,参数:{}", pageNum);
        return roomCheckInMapper.listByPageNum(pageNum);
    }

    @Override
    public List<RoomCheckInListCountVO> listCount() {
        return roomCheckInMapper.listCount();
    }
}
